﻿using ModelLayer;
using System.Collections.Generic;

namespace ControllerLayer.Dijkstras
{
    public class ReachableNodeList
    {
        private readonly List<ReachableNode> _rnList;
        private readonly List<Node> _nodes;
        private readonly Dictionary<Node, ReachableNode> _rnDictionary;

        public ReachableNodeList()
        {
            _rnList = new List<ReachableNode>();
            _nodes = new List<Node>();
            _rnDictionary = new Dictionary<Node, ReachableNode>();
        }

        public void AddReachableNode(ReachableNode rn)
        {
            if (_nodes.Contains(rn.Node))
            {
                var oldReachableNode = GetReachableNodeFromNode(rn.Node);
                if (rn.Edge.Distance < oldReachableNode.Edge.Distance)
                    oldReachableNode.Edge = rn.Edge;
            }
            else
            {
                _rnList.Add(rn);
                _nodes.Add(rn.Node);
                _rnDictionary.Add(rn.Node, rn);
            }
        }

        public List<ReachableNode> ReachableNodes
        {
            get { return _rnList; }
        }

        public void RemoveReachableNode(ReachableNode rn)
        {
            _rnList.Remove(rn);
            _nodes.Remove(rn.Node);
        }

        public bool HasNode(Node n)
        {
            return _nodes.Contains(n);
        }

        public ReachableNode GetReachableNodeFromNode(Node n)
        {
            return _rnDictionary.ContainsKey(n) ? _rnDictionary[n] : null;
        }

        public void SortReachableNodes()
        {
            _rnList.Sort();
        }

        public void Clear()
        {
            _rnList.Clear();
            _nodes.Clear();
            _rnDictionary.Clear();
        }
    }
}